package com.zy.explore.idempotent.infrastructure.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * description Knife4j配置类
 *
 * @author 中国印
 * @date 2021年07月06日20时17分07秒
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile("!prod")
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

	@Resource
	private OpenApiExtensionResolver openApiExtensionResolver;

	/**
	 * 所有接口
	 */
	@Bean(value = "apiAll")
	public Docket apiAll() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("All")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore.idempotent.interfaces.facade"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("All"));
	}

	/**
	 * API分组 版本1
	 */
	@Bean(value = "apiV1")
	public Docket apiV1() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("v1")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore.idempotent.interfaces.facade.v1"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("v1"));
	}

	/**
	 * API分组 版本2
	 */
	@Bean(value = "apiV2")
	public Docket apiV2() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("v2")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore.idempotent.interfaces.facade.v2"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("v2"));
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("Restful API 说明文档")
				.contact(new Contact("ZY", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("1.0")
				.build();
	}
}